package controller;

import entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

@Controller
@RequestMapping("/param")
public class ParamController {

    @RequestMapping("/f1")
    public void f1(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("ParamController.f1");
        System.out.println("request = " + request + ", response = " + response);
        System.out.println(request.getRemoteAddr());
        PrintWriter out=response.getWriter();

        out.println("Hello");
    }

    @RequestMapping("/f2")
    public String f2(Map map){
        System.out.println("ParamController.f2");
        map.put("message","abc");
        return "hello";
    }

    @RequestMapping("/f3")
    public void f3(String name){
        System.out.println("ParamController.f3");
        System.out.println("name = " + name);
    }

    @RequestMapping("/f4")
    public void f4(@RequestParam String name){
        System.out.println("ParamController.f4");
        System.out.println("name = " + name);
    }

    @RequestMapping("/f5")
    public void f5(@RequestParam(value = "username",defaultValue = "abc") String name){
        System.out.println("ParamController.f5");
        System.out.println("name = " + name);
    }

    @RequestMapping("/f6/{name}")
    public void f6(@PathVariable String name){
        System.out.println("ParamController.f6");
        System.out.println("name = " + name);
    }

    @RequestMapping("/f7/{username}")
    public void f7(@PathVariable("username") String name){
        System.out.println("ParamController.f7");
        System.out.println("name = " + name);
    }
    
    @RequestMapping("/f8")
    public void f8(User user, Errors errors){
        System.out.println("ParamController.f8");
        if(errors.hasErrors()){
            return;
        }

        System.out.println("user = " + user);
    }




}
